﻿/*
* BattleScene.cpp
*
* 战斗场景
*
* create date: 2014.4.10
* create by: leo
*
*/

#include "BattleScene.h"

#include <cstring>
#include "cocos-ext.h"
#include "CCLuaEngine.h"
#include "../corps/EmptyCorps.h"
#include "../user/User.h"
#include "../lua/LuaHelper.h"
#include "../heros/HeroBuilder.h"
#include "../corps/RightCorpsFormation.h"

USING_NS_CC;
USING_NS_CC_EXT;
using namespace cocos2d::ui;

CBaseScene* CBattleScene::create()
{
	CBattleScene* scene = new CBattleScene();
	if (NULL != scene && scene->init())
	{
		scene->autorelease();
		return scene;
	}

	return NULL;
}

void CBattleScene::setLeftCorps(CBaseCorps& corps)
{
	mLeftCorps = &corps;
}

void CBattleScene::setRightCorps(CBaseCorps& corps)
{
	mRightCorps = &corps;
}

CBaseCorps* CBattleScene::getEnemyCorps(CBaseCorps& corps)
{
	if (mLeftCorps == &corps)
	{
		return mRightCorps;
	}
	else if (mRightCorps == &corps)
	{
		return mLeftCorps;
	}
	return NULL;
}

void CBattleScene::prepare()
{
	CUser::getInstance().prepareEnterBattle(*this);

	// preload lua file
	CLuaHelper::getInstance().loadFile("lua/base.lua");
	CLuaHelper::getInstance().loadFile("lua/lib.lua");
	CLuaHelper::getInstance().loadFile("lua/hello.lua");

	CBaseCorps* rightCorps = new CBaseCorps();

	rightCorps->setCorpsFormation(*new CRightCorpsFormation());

	rightCorps->removeAllHero();

	CBaseHero* hero = CHeroBuilder::getInstance().createHero("zhugeliang");
//	hero->setPosition(ccp(WINSIZE_WIDTH, 0));
	rightCorps->addHero(*hero);

    hero = CHeroBuilder::getInstance().createHero("zhugeliang");
//	hero->setPosition(ccp(WINSIZE_WIDTH * 1.2, WINSIZE_HEIGHT * 0.2));
    hero->setCorpsPos(CBaseCorps::SECOND);
	rightCorps->addHero(*hero);

    hero = CHeroBuilder::getInstance().createHero("guanyu");
//	hero->setPosition(ccp(WINSIZE_WIDTH * CCRANDOM_0_1(), WINSIZE_HEIGHT * CCRANDOM_0_1()));
    hero->setCorpsPos(CBaseCorps::THIRD);
	rightCorps->addHero(*hero);

    hero = CHeroBuilder::getInstance().createHero("zhugeliang");
//	hero->setPosition(ccp(WINSIZE_WIDTH * CCRANDOM_0_1(), WINSIZE_HEIGHT * CCRANDOM_0_1()));
    hero->setCorpsPos(CBaseCorps::FOURTH);
	rightCorps->addHero(*hero);

    rightCorps->preloadRes();

	setRightCorps(*rightCorps);

	mPrepared = true;
}

bool CBattleScene::isPrepared()
{
//	CCTMXTiledMap
	return mPrepared;
}

void CBattleScene::startBattle()
{
	if (!isPrepared())
	{
		prepare();
	}
//	CCDirector::sharedDirector()->getScheduler()->scheduleScriptFunc(scheduleUpdate, 0, false);

//	CCLuaEngine::defaultEngine()->executeGlobalFunction("StartBattle");

//	CCLuaEngine::defaultEngine()->getLuaStack()->pushInt(222);
//	CCLuaEngine::defaultEngine()->getLuaStack()->executeFunctionByHandler(CLuaHelper::getInstance().getHandler("abc"), 1);
//	CCLuaEngine::defaultEngine()->getLuaStack()->clean();

	mLeftCorps->setOnDefeatListener(this, callfuncO_selector(CBattleScene::onDefeat));
	mRightCorps->setOnDefeatListener(this, callfuncO_selector(CBattleScene::onDefeat));

	mLeftCorps->startBattle(*this);
	mRightCorps->startBattle(*this);

	m_bReorderChildDirty = true;
}

void CBattleScene::addSortNode(cocos2d::CCNode* node)
{
	if (!mSortNodes.containsObject(node))
	{
		mSortNodes.addObject(node);
		((CBaseHero*)node)->addStateListener(*this);
	}
}

void CBattleScene::removeSortNode(cocos2d::CCNode* node)
{
	mSortNodes.removeObject(node);
	((CBaseHero*)node)->removeStateListener(*this);
}

void CBattleScene::onDefeat(cocos2d::CCObject* obj)
{
	if (obj == mLeftCorps)
	{
		CCLOG("lost");
		mRightCorps->winGame();
	}
	else
	{
		CCLOG("win");
		mLeftCorps->winGame();
	}
}

CBattleScene::CBattleScene(void)
: mLeftCorps(&CBaseCorps::EmptyCorps),
  mRightCorps(&CBaseCorps::EmptyCorps),
  mPrepared(false)
{
}

CBattleScene::~CBattleScene(void)
{}

bool CBattleScene::init(void)
{
	CCSprite* bg = CCSprite::create("bg.jpg");
	CENTRE(bg);
	this->addChild(bg);

//	Button* btn = ui::Button::create();
//	btn->addTouchEventListener(this, SEL_TouchEvent(CSplashScene::onTouchStart));
//	btn->setTitleText("battle");
//	CENTRE(btn);
//	this->addChild(btn);

//	UILayer* ul = UILayer::create();
	cocos2d::ui::Widget* widget = cocos2d::extension::GUIReader::shareReader()->widgetFromJsonFile("NewUI_1_1/NewUI_1.json");

	this->addChild(widget);

	return true;
}

void CBattleScene::onDied(CBaseHero& hero)
{}

void CBattleScene::onPositionChange(CBaseHero& hero)
{
	this->reorderChild(&hero, WINSIZE_HEIGHT - hero.getPositionY());
}
